import sys
import numpy

def parse_times(filename):
    times = []
    f = open(filename, 'r')
    for line in f:
        try:
            parts = line[:-1].split()
            if len(parts)==3 and parts[1]=='endrepair':
                times.append(float(parts[2]))
        except:
            print >>sys.stderr, 'error:',line
    f.close()

    y,xs = numpy.histogram(times, bins=30, new=True)
    y = y/(numpy.ones(len(y))*float(len(times)))
    y = y.cumsum()
    print xs[0], 0
    for i,x in enumerate(xs[1:]):
        print x,y[i]

def parse_totalrepairs(filename):
    num = 0
    f = open(filename, 'r')
    last = None
    for line in f:
        try:
            parts = line[:-1].split()
            last = float(parts[0])
            if (len(parts)==2 and parts[1]=='oracle') or \
               (len(parts)==4 and parts[1]=='inirepair'):
                num += 1
        except:
            print >>sys.stderr, 'error:',line
    f.close()
    print (num/last)*6*30*24*3600

def parse_oracle(filename):
    times = []
    f = open(filename, 'r')
    last = None
    for line in f:
        try:
            parts = line[:-1].split()
            if len(parts)==2 and parts[1]=='oracle':
                if last==None:
                    last = float(parts[0])
                else:
                    new = float(parts[0])
                    times.append(new-last)
                    last = new
        except:
            print >>sys.stderr, 'error:',line
    f.close()

    y,xs = numpy.histogram(times, bins=20, new=True)
    y = y/(numpy.ones(len(y))*float(len(times)))
    y = y.cumsum()
    print xs[0], 0
    for i,x in enumerate(xs[1:]):
        print x,y[i]

def parse_numnodes(filename):
    times = []
    f = open(filename, 'r')
    for line in f:
        try:
            parts = line[:-1].split()
            if len(parts)==4 and parts[1]=='inirepair':
                times.append(float(parts[2]))
        except:
            print >>sys.stderr, 'error:',line
    f.close()

    y,xs = numpy.histogram(times, bins=30, new=True)
    y = y/(numpy.ones(len(y))*float(len(times)))
    y = y.cumsum()
    print xs[0], 0
    for i,x in enumerate(xs[1:]):
        print x,y[i]

def main(mode, filename):
    if mode=='times':
        parse_times(filename)
    elif mode=='oracle':
        parse_oracle(filename)
    elif mode=='numnodes':
        parse_numnodes(filename)
    elif mode=='totalrepairs':
        parse_totalrepairs(filename)
   
if __name__=='__main__':
    if main.func_code.co_argcount != len(sys.argv)-1:
        print 'incorrect number of arguments'
    else:
        try:
            main(*sys.argv[1:])
        except KeyboardInterrupt:
            pass
